package cn.edu.nwpu.warehousemanagement.service.impl;

import cn.edu.nwpu.warehousemanagement.constantEnum.FreightStatus;
import cn.edu.nwpu.warehousemanagement.pojo.Freight;
import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.pojo.dto.*;
import cn.edu.nwpu.warehousemanagement.dao.FreightRepository;
import cn.edu.nwpu.warehousemanagement.service.FreightService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 主要为Freight查找服务的实现
 * @author LSR
 */
@Service
public class FreightServiceImpl implements FreightService {

    private final FreightRepository freightRepository;

    public FreightServiceImpl(FreightRepository freightRepository) {
        this.freightRepository = freightRepository;
    }

    @Override
    public Result<Page<Freight>> getAllFreights(PageDTO pageDTO) {
        // 设置分页参数，每页20条
        Pageable pageable = PageRequest.of(pageDTO.getPageNum() - 1, pageDTO.getPageSize());
        // 查询未删除的所有货物
        Page<Freight> freightPage = freightRepository.findAllByDeletedFalse(pageable);
        return Result.success(freightPage);
    }

    @Override
    public Result<Page<Freight>> getInStockFreights(PageDTO pageDTO) {
        // 设置分页参数，每页20条
        Pageable pageable = PageRequest.of(pageDTO.getPageNum() - 1, pageDTO.getPageSize());
        // 查询状态为在库且未删除的货物
        Page<Freight> freightPage = freightRepository.findByStatusAndDeletedFalse(FreightStatus.IN_STOCK, pageable);
        return Result.success(freightPage);
    }

    @Override
    public Result<List<Freight>> getInStockFreights() {
        // 查询状态为在库且未删除的所有货物（不分页）
        List<Freight> freights = freightRepository.findByStatusAndDeletedFalse(FreightStatus.IN_STOCK);
        return Result.success(freights);
    }

    @Override
    public Result<Page<Freight>> getOutboundFreights(PageDTO pageDTO) {
        // 设置分页参数，每页20条
        Pageable pageable = PageRequest.of(pageDTO.getPageNum() - 1, pageDTO.getPageSize());
        // 查询状态为出库且未删除的货物
        Page<Freight> freightPage = freightRepository.findByStatusAndDeletedFalse(FreightStatus.SHIPPED, pageable);
        return Result.success(freightPage);
    }

    @Override
    public Result<List<Freight>> getOutboundFreights() {
        // 查询状态为出库且未删除的所有货物（不分页）
        List<Freight> freights = freightRepository.findByStatusAndDeletedFalse(FreightStatus.SHIPPED);
        return Result.success(freights);
    }

    @Override
    public Result<Freight> getFreightById(IDDTO idDTO) {
        // 根据ID查询未删除的货物
        return freightRepository.findByIdAndDeletedFalse(idDTO.getId())
                .map(Result::success)
                .orElse(Result.error("未找到该货物"));
    }

    @Override
    public Result<?> getFreightsByName(NameDTO nameDTO, PageDTO pageDTO) {
        if (pageDTO != null && StringUtils.hasText(pageDTO.getPageNum() + "")) {
            // 带分页的名称查询
            Pageable pageable = PageRequest.of(pageDTO.getPageNum() - 1, pageDTO.getPageSize());
            Page<Freight> freightPage = freightRepository.findByProductNameContainingAndDeletedFalse(nameDTO.getName(), pageable);
            return Result.success(freightPage);
        } else {
            // 不带分页的名称查询
            List<Freight> freights = freightRepository.findByProductNameContainingAndDeletedFalse(nameDTO.getName());
            return Result.success(freights);
        }
    }


    @Override
    public Result<Page<Freight>> getInStockFreightsByShelfIdWithPage(Long shelfId, PageDTO pageDTO) {
        // 设置分页参数（每页20条）
        Pageable pageable = PageRequest.of(pageDTO.getPageNum() - 1, pageDTO.getPageSize());

        // 查询：指定货架 + IN_STOCK状态 + 未删除
        Page<Freight> freightPage = freightRepository.findByShelfIdAndStatusAndDeletedFalse(
                shelfId,
                FreightStatus.IN_STOCK,
                pageable
        );

        return Result.success(freightPage);
    }

    @Override
    public Result<List<Freight>> getInStockFreightsByShelfId(Long shelfId) {
        // 查询：指定货架 + IN_STOCK状态 + 未删除（不分页）
        List<Freight> freights = freightRepository.findByShelfIdAndStatusAndDeletedFalse(
                shelfId,
                FreightStatus.IN_STOCK
        );

        return Result.success(freights);
    }

    @Override
    public Result<Page<Freight>> searchInStockFreightsByProductName(String productName, int pageNum, int pageSize) {
        // 创建分页请求 (每页20条结果)
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        //执行查询
        Page<Freight> freightPage = freightRepository.findByProductNameContainingAndInStock(productName, pageable);

        return Result.success(freightPage);
    }

    @Override
    public Result<Page<Freight>> searchOutStockFreightsByProductName(String productName, int pageNum, int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        Page<Freight> freightPage = freightRepository.findByProductNameContainingAndOutStock(productName, pageable);

        return Result.success(freightPage);
    }

    @Override
    public Result<Page<Freight>> getInStockFreightsByShelfAndProductName(
            Long shelfId,
            String productName,
            int pageNum,
            int pageSize) {
        // 创建分页请求 (每页20条结果)
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        // 执行查询
        Page<Freight> freightPage = freightRepository.findFreightsByShelfAndStatusAndProductName(
                shelfId,
                FreightStatus.IN_STOCK,
                productName,
                pageable);

        return Result.success(freightPage);
    }

    @Override
    public Result<Page<Freight>> getOutStockFreightsByShelfAndProductName(
            Long shelfId,
            String productName,
            int pageNum,
            int pageSize) {
        // 创建分页请求 (每页20条结果)
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);

        // 执行查询
        Page<Freight> freightPage = freightRepository.findFreightsByShelfAndStatusAndProductName(
                shelfId,
                FreightStatus.SHIPPED,
                productName,
                pageable);

        return Result.success(freightPage);
    }
}